// pages/index/property/property.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        RepairList: [],
        fileList: [],
        radio: null,
        tpeylist: [],
        getimagelist:[],
        type: '请选择报修类型',
        textareaValue: null,
        maxNumber: 200,//可输入最大字数
        number: 0,//已输入字数
        phone: null,
        room_id:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            theme: app.globalData.theme,
            room_id: app.globalData.room_id
        })
        this.getEventType()
        this.getPhoneNum()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 描述 输入框中的值
     */
    getTextareaValue: function (e) {
        const than = this
        this.setData({
            textareaValue: e.detail.value,
            number: e.detail.value.length//获取textarea的内容长度
        });
        log.e("输入的内容" + than.data.textareaValue)
    },
    /**
     * 电话号码 输入框中的值
     */
    getTextValue: function (e) {
        const than = this
        than.setData({
            phone: e.detail.value
        })
        log.e("输入的内容" + than.data.phone)
    },
    /**
     * 获取电话号码
     */
    getPhoneNum: function () {
        const than = this
        http.get("people_info/people", {
            people_id:app.globalData.userInfo.people_id,//'8a8080d373991a3b0173991a3bce0000'
        }).then((res) => {
            than.setData({
                phone: res.data.data.phone
            })
        })
    },
    /**
     * 事件类别确定按钮
     * @param e
     */
    getEvent: function (e) {
        this.setData({
            show: false
        });

    },
    /**
     * 事件类型弹出层 关闭
     */
    onClose() {
        this.setData({
            show: false
        });
    },
    /**
     * 事件类型弹出层 显示
     */
    getType: function () {
        this.setData({
            show: true
        });
    },
    /**
     * 弹出层当前选中项的 name
     * @param event
     */
    onChange(event) {
        log.e(JSON.stringify(event.detail))
        this.setData({
            radio: event.detail,
        });
    },
    onClick(event) {
        const {name} = event.currentTarget.dataset;
        const dicValue = event.currentTarget.dataset.dicvalue;
        this.setData({
            radio: name,
            type: dicValue
        });
    },
    /**
     * 保修类别
     * @param event
     */
    getEventType: function (event) {
        const than = this
        http.get("dic/getDicByName", {
            name: 'D0061'
        }).then((res) => {
            if (res.data.code == 200) {
                than.setData({
                    tpeylist: res.data.data,
                })
            }
        })
    },
    /**
     * 图片上传
     */
    afterRead: function (e) {
        const than = this
        for (let i = 0; i < e.detail.file.length; i++) {
            than.data.fileList.push({
                url: e.detail.file[i].path,
                name: e.detail.file[i].size
            })
        }
        than.setData({
            fileList: than.data.fileList
        })
    },

    /**
     * 删除图片
     */
    deleteImage: function (e) {
        const than = this
        const index = e.detail.index
        const deleteData = this.data.fileList.splice(index, 1)
        than.setData({
            fileList: than.data.fileList
        })

    },
    setproperty: function (event) {
        const than = this
        const fileListproperty = than.data.fileList
        const fileArray = []
        //上传附件
        if (fileListproperty != null) {
            for (let i = 0; i < fileListproperty.length; i++) {
                http.upFile(fileListproperty[i].url, "property")
                    .then((res) => {
                        fileArray[i] = res.data.url
                        log.e(JSON.stringify(fileArray))
                    })
            }
        }
        if (than.data.radio != null) {
            if (than.data.textareaValue != null) {
                if (than.data.phone != null) {
                    Dialog.confirm({
                        title: "物业保修",
                        message: "确定要上报吗？"
                    }).then(() => {
                        //确认按钮
                        Toast.loading({
                            duration: 2000,
                            forbidClick: true,
                            message: '正在上报，请稍等！'
                        })
                        http.postHeader("repair/report", {
                            'Authorization': app.globalData.accessToken
                        }, {
                            people_id:app.globalData.userInfo.people_id,//'8a8080d373991a3b0173991a3bce0000'
                            type: than.data.radio,
                            phone: than.data.phone,
                            room_id: than.data.room_id,//'ff8080815d7f6880015d7f6ff4a102c7',//房间id
                            content: than.data.textareaValue,
                            picture: fileArray
                        }).then((res) => {
                            if (res.data.code == 200) {
                                Toast.success({
                                    message: res.data.msg
                                })
                                setTimeout(() => {
                                    wx.navigateBack({
                                        delta: 2
                                    })
                                }, 2000)
                            }else {
                                Toast.fail({
                                    message: "提交失败，错误代码："+res.data.msg+",请联系管理员!"
                                })
                            }
                        }, (err) => {
                            //请求失败
                            Toast.clear()
                            Toast.fail({
                                message: "上报失败"
                            })
                        })

                    }).catch(() => {

                    })
                } else {
                    Toast.fail({
                        message: '请输入联系电话！'
                    })
                }
            } else {
                Toast.fail({
                    message: '请输入描述信息！'
                })
            }
        } else {
            Toast.fail({
                message: '请选择报修类型！'
            })
        }
    },
    /**
     * 当前激活的标签改变时触发
     * @param event
     */
    onChange1(event) {
        const than = this
        const num = event.detail.name
        if (num == 0) {

        } else if (num == 1) {
            than.getRepairList()
        }
    },
    /**
     * 请求物业保修列表
     * @param event
     */
    getRepairList:function (event) {
        const than=this
        http.get("repair/page",{
            people_id:app.globalData.userInfo.people_id,//'8a8080d373991a3b0173991a3bce0000'
            room_id: than.data.room_id,//'ff8080815d7f6880015d7f6ff4a102c7',//房间id
        }).then((res)=>{
            if (res.data.code==200){
                res.data.data.list.forEach(item => {
                    let list = []
                    item.picture_list.forEach(itemB => {
                        list.push(itemB)
                    })
                    item.picture_list = list
                })
                than.setData({
                    RepairList:res.data.data.list,
                })
            }else {

            }
        })
    },
    Repair:function (event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: '../../index/property/propertydetails/propertydetails?id='+id
        })
    }
})